package com.yaodian100.ec.report.rank.web;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.service.ProductService;

public class ProductSelectAction extends ActionSupport implements Preparable {

	private static final String PRODUCT_SEARCH = "productSearch";
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	private String categoryId;
	private Category category;
	
	private String productId;
	private String productName;
	private String productType;
	private String productStatus;
	private String supplierId;
	private String brandId;
	
	private List<Product> productList;

	@Autowired
	private DomainService<Menu> menuService;
	@Autowired
	private CategoryService categoryService;
	@Autowired
	private ProductService productService;

	private Menu attributeMenu;
	private SimplePager pager;

	public String productSearch() {
		logger.debug(productId);
		logger.debug(categoryId);
		Category category = categoryService.get(Long.parseLong(categoryId));
		setCategory(category);
		String categoryPath = category.getCategoryPath();
		Number productListSize = productService.getProductOnShelfPageableListSize(productId, productName, productType, productStatus, supplierId, categoryPath, brandId);
		pager.setTotalSize(productListSize.intValue());
		productList = productService.getProductOnShelfPageableList(productId, productName, productType,
				productStatus, supplierId, categoryPath, brandId, pager.getCurrentPage()
						* pager.getPageRecord(), pager.getPageRecord());

		
		return PRODUCT_SEARCH;
	}
	
	@Override
	public void prepare() throws Exception {
		attributeMenu = menuService.get("Product.Attribute");
		if (pager == null) {
			pager = new SimplePager();
		}
		pager.setPageRecord(20);// defaut 20
	}

	public String getProductId() {
		return this.productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getProductName() {
		return this.productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}

	public String getProductType() {
		return this.productType;
	}
	public void setProductType(String productType) {
		this.productType = productType;
	}

	public String getProductStatus() {
		return this.productStatus;
	}
	public void setProductStatus(String productStatus) {
		this.productStatus = productStatus;
	}

	public String getSupplierId() {
		return this.supplierId;
	}
	public void setSupplierId(String supplierId) {
		this.supplierId = supplierId;
	}

	public String getBrandId() {
		return this.brandId;
	}
	public void setBrandId(String brandId) {
		this.brandId = brandId;
	}

	public DomainService<Menu> getMenuService() {
		return this.menuService;
	}
	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public String getCategoryId() {
		return this.categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public ProductService getProductService() {
		return this.productService;
	}
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public CategoryService getCategoryService() {
		return this.categoryService;
	}
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public Menu getAttributeMenu() {
		return this.attributeMenu;
	}
	public void setAttributeMenu(Menu attributeMenu) {
		this.attributeMenu = attributeMenu;
	}

	public SimplePager getPager() {
		return this.pager;
	}
	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public List<Product> getProductList() {
		return this.productList;
	}
	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public Category getCategory() {
		return this.category;
	}
	public void setCategory(Category category) {
		this.category = category;
	}

}
